package distriKub;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
		
	public static int clientsConnected = 0;
	public static int MaxPlayersInTeam = 4;

	private static Database myDB = new Database();
	
	public static void main(String[] args) {

		List<List<ServerPlayer>> playingGroups = new ArrayList<List<ServerPlayer>>(); 
		
		boolean listeningState = true;
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(ComRules.serverPort);
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println("DistriKub server is starting up - let's play...\n");

		while (listeningState == true) {
			try {
				Socket clientSocket = serverSocket.accept();
				ServerThread clientThread = new ServerThread(clientSocket, playingGroups);
				clientThread.bindToDatabase(myDB);
				Thread clientHandler = new Thread(null,clientThread,"Server Thread for Client" + clientsConnected++);
				clientHandler.start();
				
				clientsConnected++;
				
			} catch (IOException IOEX) {
				System.out.println("\n===> Server.java ===> Client disconnected or connection lost (IOException)\n");
				clientsConnected--;
			} // End of catch block

		} // End of while loop block

	}
}
